"""
采用restapi进行分词
"""
import os
from flask_utility.utility import RestApiProcess, Flask
from logger_utility.loggconfig import getLogger, LoggerConfig, DEBUG
from nlp_utility.ltp.ltpwrap import LtpWrapper

LOGS_PATH = '.'
LOG_FILE_BASE = 'LtpWrap.Runtime.RestApi.log'
# 先配置日志，后面再执行本项目相关的代码
logger = getLogger('Server')
logger_config = LoggerConfig(os.path.join(LOGS_PATH, LOG_FILE_BASE))
log_to_conso = os.environ.get('LogTerm')
logger_config.config(to_screen=log_to_conso, level=DEBUG)

app = Flask(__name__)
rest_api_process = RestApiProcess(logger)

ltp = LtpWrapper()


@app.route('/segment', methods=['POST', 'GET'])
@rest_api_process.wrap
def process_segment(in_data):
    """
    实际查找敏感词的函数
    :param in_data:
    :return:
    """
    content = in_data.get('content')
    ret_dict = {'data': list(ltp.segment(content))}
    return ret_dict


@app.route('/postag', methods=['POST', 'GET'])
@rest_api_process.wrap
def process_postag(in_data):
    """
    实际查找敏感词的函数
    :param in_data:
    :return:
    """
    content = in_data.get('content')
    ret_dict = {'data': [list(i) for i in ltp.postag(content)]}
    return ret_dict


@app.route('/ner', methods=['POST', 'GET'])
@rest_api_process.wrap
def process_ner(in_data):
    """
    实际查找敏感词的函数
    :param in_data:
    :return:
    """
    content = in_data.get('content')
    ret_dict = {'data': [list(i) for i in ltp.ner(content)]}
    return ret_dict


@app.route('/parse', methods=['POST', 'GET'])
@rest_api_process.wrap
def process_parse(in_data):
    """
    实际查找敏感词的函数
    :param in_data:
    :return:
    """
    content = in_data.get('content')
    ret_dict = {'data': [list(i) for i in ltp.parse(content)]}
    return ret_dict


@app.route('/label', methods=['POST', 'GET'])
@rest_api_process.wrap
def process_label(in_data):
    """
    实际查找敏感词的函数
    :param in_data:
    :return:
    """
    content = in_data.get('content')
    ret_dict = {'data': [list(i) for i in ltp.label(content)]}
    return ret_dict


if __name__ == '__main__':
    """
    PYTHONPATH='/media/spiriy/OS/D/coversensitive:/media/spiriy/OS/D/NewCommonUtility' LogTerm=1 python2 server.py
    PYTHONPATH='/media/spiriy/OS/D/coversensitive:/media/spiriy/OS/D/NewCommonUtility' gunicorn -b 127.0.0.1:10064 -w 2 -k gevent server:app
    """
    app.run(debug=False, host='0.0.0.0', port=10077, threaded=True)